<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Seller\Controllers;


use App\Eloquent\Ygt\Company;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\ProductFields;
use App\Eloquent\Ygt\SellerBill;
use App\Eloquent\Ygt\SellerCompany;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class CompanyController extends Controller
{
    public $userId        = 248;
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $page               = request('page',1);
        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = [
            'ygt_seller_company.user_id'=>$userId
        ];
        $limit              = 10;
        $offset             = ($page-1)*$limit;
        $collection         = Company::getJoinList($where);

        $lists              = Func::getLetterList($collection);




        //先给空值字段，防止出现null值
        foreach ($lists as $key => $tmpRow){
            foreach ($tmpRow['list'] as $key2 => $tmpRow2){
//                $tmpCompanyId = $tmpRow2['id'];
//                //获取用户对应的供应商ID
//                $tmpSellerCompanyRow = \App\Eloquent\Ygt\SellerCompany::where(['user_id'=>$userId,'company_id'=>$tmpCompanyId])->first();
//                if($tmpSellerCompanyRow){
//                    $tmpSellerCompanyId = $tmpSellerCompanyRow['id'];
//
//                    //最近订单（时间）
//                    $tmpPurchaseRow = \App\Eloquent\Ygt\Purchase::where(['company_id'=>$tmpCompanyId,'supplier_id'=>$tmpSellerCompanyId])->max('created_at');
//                    $lastOrder = '';//最近订单时间
//                    if($tmpPurchaseRow){
//                        $lastOrder = date('Y-m-d',$tmpPurchaseRow);
//                    }
//
//                    ////暂定type=1和type=2为应收款，type为3为应付款
//                    //最近收款（时间、金额）
//                    $lastMoney = '';
//                    $tmpLastSellerBillRow = \App\Eloquent\Ygt\SellerBill::where(['company_id'=>$tmpCompanyId,'seller_company_id'=>$tmpSellerCompanyId])->whereRaw('total_money != now_money')->whereIn('type',[1,2])->orderBy('updated_at','desc')->first();
//                    if($tmpLastSellerBillRow){
//                        $lastMoney = $tmpLastSellerBillRow['total_money']-$tmpLastSellerBillRow['now_money'];
//                        $lastMoneyDate = date('Y-m-d',($tmpLastSellerBillRow->toArray())['updated_at']);
//                        $lastMoney = "¥{$lastMoney}({$lastMoneyDate})";
//                    }
//
//                    //应收款


//                    $lists[$key]['list'][$key2]['last_order'] = $lastOrder;
//                    $lists[$key]['list'][$key2]['last_money'] = $lastMoney;

//                }else{
//                    $lists[$key]['list'][$key2]['last_order'] = '';
//                    $lists[$key]['list'][$key2]['last_money'] = '';
//                    $lists[$key]['list'][$key2]['total_money'] = '';
//                }


            $lists[$key]['list'][$key2]['last_order'] = '';
            $lists[$key]['list'][$key2]['last_money'] = '';
            $lists[$key]['list'][$key2]['total_money'] = '';


                //总额


            }
        }

//        $letterArr          = $collection->pluck('first_letter')->all();
//        $lists              = [];
//        if($letterArr)
//        {
//            $letterArr      = array_unique($letterArr);
//            sort($letterArr);
//            $listArr            = $collection->groupBy('first_letter')->toArray();
//            foreach($letterArr as $key=>$val)
//            {
//                $lists[$key]['letter']      = $val;
//                $lists[$key]['list']       = $listArr[$val];
//            }
//        }
        return $lists;
    }
    public function detail()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $companyId          = request('id',0);
        if(!$userId)
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = ['user_id'=>$userId,'company_id'=>$companyId];
        $sellerCompany      = SellerCompany::getInfo($where);
        if(!$sellerCompany)
        {
            xThrow(ERR_UNKNOWN,'你在该厂的信息出错');
        }
        $where              = ['id'=>$companyId];
        $info               = Company::getInfo($where);
        if(!$info)
        {
            xThrow( ERR_PARAMETER,'信息出错' );
        }
        $company            = $info->toArray();
        $result             = [
            'company'=>$company
        ];
        return $result;
    }
}